{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.2]\n",
      "[0.2 0.4]\n",
      "[0.2 0.4 0.6 0.8]\n",
      "[0.2 0.4 0.6 0.8]\n",
      "[0.2 0.4 0.6 0.8 1.  1.2 1.4 1.6 1.8]\n",
      "[0.2 0.4 0.6 0.8]\n",
      "[0.2 0.4 0.6 0.8 1.  1.2 1.4 1.6 1.8]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "print(np.arange(0.2, 0.4, 0.2))\n",
    "print(np.arange(0.2, 0.6, 0.2))\n",
    "print(np.arange(0.2, 0.8, 0.2))\n",
    "print(np.arange(0.2, 1, 0.2))\n",
    "print(np.arange(0.2, 2, 0.2))\n",
    "print(np.arange(0.2, round(1.0, 1), 0.2))\n",
    "print(np.arange(0.2, 2.0, 0.2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "ls = [10, 9, 1, 12, 23]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "ls = np.array(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "ls.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  9, 10, 12, 23])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum([1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "total_value = 3237480\n",
    "\n",
    "last_price = 5396.8\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16187.4"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_value / 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "5393.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 9, 10, 12, 23]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "float"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(round(1.0, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate list (not \"int\") to list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-17-bbd646006d04>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: can only concatenate list (not \"int\") to list"
     ]
    }
   ],
   "source": [
    "list(np.array([1, 2, 3])) + 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.arange?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 0 0 7 7 9 9]\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "def bag(weight,values,weight_cont):\n",
    "    num = len(weight)\n",
    "    weight.insert(0,0)\n",
    "    values.insert(0,0)\n",
    "    bag = np.zeros((num+1,weight_cont+1),dtype=np.int)\n",
    "    for i in range(1,num+1):\n",
    "        for j in range(1,weight_cont+1):\n",
    "            if j >= weight[i]:\n",
    "                bag[i][j] = max(bag[i-1][j],bag[i-1][j-weight[i]]+values[i])\n",
    "            else:\n",
    "                bag[i][j] = bag[i][j-1]\n",
    "    print(bag[-1])\n",
    "    return bag[-1][-1]\n",
    "if __name__ == '__main__':\n",
    "    weight = [2, 3, 4, 5]\n",
    "    values = [3, 4, 5, 6]\n",
    "    weight_cont = 8\n",
    "    re = bag(weight,values,weight_cont)\n",
    "    print(re)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pack1(w, v, C): #每个东西只能选择一次\n",
    "    dp = [[0 for _ in range(C+1)] for _ in range(len(w)+1)]\n",
    "    for i in range(1, len(w)+1):\n",
    "        for j in range(1, C+1):\n",
    "            if j < w[i-1]: #如果剩余容量不够新来的物体 直接等于之前的\n",
    "                dp[i][j] = dp[i-1][j]\n",
    "            else:\n",
    "                dp[i][j] = max(dp[i-1][j], dp[i-1][j-w[i-1]]+ v[i-1])\n",
    "    print(dp[len(w)])\n",
    "    return dp[len(w)][C]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 0, 3, 4, 5, 7, 8, 9, 10]\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "if __name__ == '__main__':\n",
    "    weight = [2, 3, 4, 5]\n",
    "    values = [3, 4, 5, 6]\n",
    "    \n",
    "    weight = [5, 2, 3, 4]\n",
    "    values = [6, 3, 4, 5]\n",
    "    weight_cont = 8\n",
    "    re = pack1(weight,values,weight_cont)\n",
    "    print(re)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  0  0  0  0  0  0  0]\n",
      "  [ 0  0  0  0  0  0  0  0]\n",
      "  [ 0  0  0  0  0  0  0  0]\n",
      "  [ 0  0  0  0  0  0  0  0]\n",
      "  [ 0  0  0  0  0  0  0  0]\n",
      "  [ 0  0  0  0  0  0  0  0]\n",
      "  [ 0  0  0  0  0  0  0  0]\n",
      "  [ 0  0  0  0  0  0  0  0]]\n",
      "\n",
      " [[ 0  0  0  0  0  0  0  0]\n",
      "  [ 0  3  3  3  3  3  3  3]\n",
      "  [ 0  3  3  3  3  3  3  3]\n",
      "  [ 0  3  3  3  3  3  3  3]\n",
      "  [ 0  3  3  3  3  3  3  3]\n",
      "  [ 0  3  3  3  3  3  3  3]\n",
      "  [ 0  3  3  3  3  3  3  3]\n",
      "  [ 0  3  3  3  3  3  3  3]]\n",
      "\n",
      " [[ 0  0  0  0  0  0  0  0]\n",
      "  [ 0  5  5  5  3  5  5  5]\n",
      "  [ 0  3  3  3  3  3  3  3]\n",
      "  [ 0  5  5  5  3  5  5  5]\n",
      "  [ 0  5  5  5  3  5  5  5]\n",
      "  [ 0  5  5  5  3  5  5  5]\n",
      "  [ 0  5  5  5  3  5  5  5]\n",
      "  [ 0  5  5  5  3  5  5  5]]\n",
      "\n",
      " [[ 0  0  0  0  0  0  0  0]\n",
      "  [ 0 10 10  5 10 10 10  8]\n",
      "  [ 0 10 10  5 10 10 10  8]\n",
      "  [ 0 10 10  5 10 10 10  8]\n",
      "  [ 0  5  5  5  5  5  5  5]\n",
      "  [ 0 10 10  5 10 10 10  8]\n",
      "  [ 0  8  8  5  8  8  8  8]\n",
      "  [ 0 10 10  5 10 10 10  8]]]\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "w = [3, 2, 4]\n",
    "b = [2, 4, 3]\n",
    "v = [3, 2, 5]\n",
    "w_most = 7\n",
    "b_most = 7\n",
    "\n",
    "def bag_0_1(w, b, v, w_most, b_most):\n",
    "    bag_num = len(w)\n",
    "    w.insert(0, 0)\n",
    "    b.insert(0, 0)\n",
    "    v.insert(0, 0)\n",
    "    dp_table = np.zeros((bag_num+1, w_most+1, b_most+1), np.int)\n",
    "    for i in range(1, bag_num+1):\n",
    "        for j in range(1, w_most+1):\n",
    "            for k in range(1, b_most+1):\n",
    "                if w[i] <= w_most and b[i] <= b_most:\n",
    "                    dp_table[i][j][k] = max(dp_table[i-1][j][k], dp_table[i-1][j-w[i]][k-b[i]] + v[i])\n",
    "                else:\n",
    "                    dp_table[i][j][k] = dp_table[i-1][j][k]\n",
    "    return dp_table\n",
    "a = bag_0_1(w, b, v, w_most, b_most)\n",
    "print(a)\n",
    "print(a.max())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "26\n"
     ]
    }
   ],
   "source": [
    "C1 = [3,2,6,7,1,4,9,5]\n",
    "C2 = [6,2,4,6,7,3,8,5]\n",
    "V = [6,3,5,8,3,1,6,9]\n",
    "\n",
    "#Count = [3,5,1,9,3,5,6,8]\n",
    "target1 = 20\n",
    "target2 = 25\n",
    "n = len(C1)\n",
    "F = [[0] * (target2+1) for i in range(0,target1+1)]\n",
    "for i in range(0,n):\n",
    "    for j in reversed(range(C1[i],target1+1)):\n",
    "        for m in reversed(range(C2[i],target2+1)):#逆序遍历\n",
    "            F[j][m] = max(F[j][m],F[j-C1[i]][m-C2[i]] + V[i])\n",
    "print (F[target1][target2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "total_value = (5400.6 * 2 + 5400.8 * 1 + 5401 * 1 + 5402.8 * 2) * 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6481720.0"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "mid_price = round(6481720.0 / 6 / 200, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8.6"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round((5400.6 * 2 + 5400.8 * 1 + 5401 * 1 + 5402.8 * 2) - 5400 * 6, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5401.4"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mid_price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.19999999999933638"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5401.4 % 0.4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "52 % 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-16-07ab5b644985>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[0mmid_price\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;36m0.2\u001b[0m \u001b[1;33m!=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m     \u001b[0mmid_price\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m0.1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "while mid_price % 0.2 != 0:\n",
    "    mid_price += 0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mid_price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "p_ls = list(np.arange(5400.6, 5400.6 + 10, 0.2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "p_ls = [round(i, 1) for i in p_ls]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5400.6,\n",
       " 5400.8,\n",
       " 5401.0,\n",
       " 5401.2,\n",
       " 5401.4,\n",
       " 5401.6,\n",
       " 5401.8,\n",
       " 5402.0,\n",
       " 5402.2,\n",
       " 5402.4,\n",
       " 5402.6,\n",
       " 5402.8,\n",
       " 5403.0,\n",
       " 5403.2,\n",
       " 5403.4,\n",
       " 5403.6,\n",
       " 5403.8,\n",
       " 5404.0,\n",
       " 5404.2,\n",
       " 5404.4,\n",
       " 5404.6,\n",
       " 5404.8,\n",
       " 5405.0,\n",
       " 5405.2,\n",
       " 5405.4,\n",
       " 5405.6,\n",
       " 5405.8,\n",
       " 5406.0,\n",
       " 5406.2,\n",
       " 5406.4,\n",
       " 5406.6,\n",
       " 5406.8,\n",
       " 5407.0,\n",
       " 5407.2,\n",
       " 5407.4,\n",
       " 5407.6,\n",
       " 5407.8,\n",
       " 5408.0,\n",
       " 5408.2,\n",
       " 5408.4,\n",
       " 5408.6,\n",
       " 5408.8,\n",
       " 5409.0,\n",
       " 5409.2,\n",
       " 5409.4,\n",
       " 5409.6,\n",
       " 5409.8,\n",
       " 5410.0,\n",
       " 5410.2,\n",
       " 5410.4]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p_ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 已知有一个价格序列：[p1, p2, p3, p4, ..., pn]\n",
    "# 且已知总价值 total_value\n",
    "# 求：将这些价格组合为总价值，有几种组合方法——每个价格可重复利用次数 < 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pack2(w, v, C):    \n",
    "    dp = [0 for _ in range(c+1)]\n",
    "    for i in range(1, len(w)+1):\n",
    "        for j in (range(1, c+1)):\n",
    "            if w[i-1] <= j:\n",
    "                dp[j] = max(dp[j], dp[j-w[i-1]]+v[i-1])\n",
    "    return dp[c]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pack2(w, v, C): #每个东西能选择多次 完全背包问题\n",
    "    dp = [[0 for _ in range(C+1)] for _ in range(len(w)+1)]\n",
    "    for i in range(1, len(w)+1):\n",
    "        for j in range(1, C+1):\n",
    "            if j < w[i-1]:\n",
    "                dp[i][j] = dp[i-1][j]\n",
    "            else:\n",
    "                dp[i][j] = max(dp[i-1][j], dp[i][j-w[i-1]]+ v[i-1])\n",
    "    for i in dp:\n",
    "        print(i)\n",
    "        \n",
    "pack2([2,3,4,5], [3,4,5,6], 8)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0: 0, 1: 1, 2: 2, 3: 1, 4: 2, 5: 1, 6: 2}\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "def dynamic(amount):\n",
    "    num=[1,3,5]\n",
    "#设置一个字典存储{钱数，硬币个数}\n",
    "    dict={0:0}\n",
    "    for i in range(1,amount+1):\n",
    "#硬币个数肯定不会大于钱数，我们设置为amount+1，如果后期没有匹配则值还为amount+1，比较好判断\n",
    "        dict[i] = amount+1\n",
    "        for j in num:\n",
    "            if j <= i:\n",
    "#最优子结构 状态转移方程   边界\n",
    "                dict[i] = min(dict[i-1]+1, dict[i - j] + 1)\n",
    "    if dict[amount] == amount+1:\n",
    "        return -1\n",
    "    else:\n",
    "        print(dict)\n",
    "        print(dict[amount])\n",
    "dynamic(6)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pack3(w, v, s, c):\n",
    "    dp = [0 for _ in range(c+1)]\n",
    "    for i in range(1, len(w)+1):\n",
    "        for j in reversed(range(1, c+1)):\n",
    "            for k in range(s[i-1] + 1):\n",
    "                if k*w[i-1] <= j:\n",
    "                    dp[j] = max(dp[j], dp[j-k*w[i-1]]+k*v[i-1])\n",
    "    return dp[c]   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "w= [2,3,4,5]\n",
    "v = [3,4,5,6] \n",
    "s = [1,1,1,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack3(w, v, s, 8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32408.6"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5400.6 * 2 + 5400.8 + 5401 + 5402.8 * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "total_price = 5400.6 * 2 * 200 + 5400.8 * 1 * 200 + 5401 * 1 * 200 + 5402.8 * 2 * 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6481720.0"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "avg_price = round(total_price / 6 / 200, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5401.4"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "avg_price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "price_ls = np.arange(5400.2, 5410.2, 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5400.2, 5400.4, 5400.6, 5400.8, 5401. , 5401.2, 5401.4, 5401.6,\n",
       "       5401.8, 5402. , 5402.2, 5402.4, 5402.6, 5402.8, 5403. , 5403.2,\n",
       "       5403.4, 5403.6, 5403.8, 5404. , 5404.2, 5404.4, 5404.6, 5404.8,\n",
       "       5405. , 5405.2, 5405.4, 5405.6, 5405.8, 5406. , 5406.2, 5406.4,\n",
       "       5406.6, 5406.8, 5407. , 5407.2, 5407.4, 5407.6, 5407.8, 5408. ,\n",
       "       5408.2, 5408.4, 5408.6, 5408.8, 5409. , 5409.2, 5409.4, 5409.6,\n",
       "       5409.8, 5410. ])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "price_ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [],
   "source": [
    "total_value = 6481720"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dp = [0 for _ in range(c+1)]\n",
    "for i in range(1, len(w)+1):\n",
    "    for j in reversed(range(1, c+1)):\n",
    "        for k in range(s[i-1] + 1):\n",
    "            if k*w[i-1] <= j:\n",
    "                dp[j] = max(dp[j], dp[j-k*w[i-1]]+k*v[i-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "price_add = total_value / 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8.599999999998545"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "price_add - 5400 * 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2. , 2.2, 2.4,\n",
       "       2.6, 2.8, 3. , 3.2, 3.4, 3.6, 3.8, 4. , 4.2, 4.4, 4.6, 4.8, 5. ,\n",
       "       5.2, 5.4, 5.6, 5.8, 6. , 6.2, 6.4, 6.6, 6.8, 7. , 7.2, 7.4, 7.6,\n",
       "       7.8, 8. , 8.2, 8.4, 8.6])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(0.0, 8.8, 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "for ind, price in enumerate(price_ls):\n",
    "    \n",
    "    ind += 1\n",
    "    \n",
    "    for j in reversed(range(1, c+1))\n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <stdio.h>\n",
    "const int num = 3; //钱币的面值数\n",
    "int value[num] = {1,7,11}; //钱币的面值\n",
    " \n",
    "void change(int n) { //找钱方法\n",
    "\tint money[n+1] = {0}, min, note, sum; \n",
    "\tint record[n+1] = {0}, num_value[num] = {0}; //record[i]记录i块钱最后用哪张面值的钞票找出，num_value根据record计算出找钱方案\n",
    "\tfor (int i = 1; i <= n; i++) {\n",
    "\t\tmin = n;\n",
    "\t\tfor (int j = 0; j < num; j++) { \n",
    "\t\t\tif (i >= value[j] && min > money[i-value[j]]) {\n",
    "\t\t\t\tmin = money[i-value[j]]; //在前面几个找钱方案中找出最小的值 \n",
    "\t\t\t\tnote = j; //记录用了哪张钞票 \n",
    "\t\t\t}\n",
    "\t\t}\n",
    "\t\tmoney[i] = min+1; \n",
    "\t\trecord[i] = value[note];\n",
    "\t}\n",
    "\tprintf(\"最少钱币张数: %d\\n\", money[n]);\n",
    "\tprintf(\"找钱方案:\\n\");\n",
    "\tprintf(\"面值\t张数\\n\");\n",
    "\tsum = n;\n",
    "\twhile (sum > 0) {//有n元钱时，最后用的钞票面值为record[n] \n",
    "\t\tfor (int i = 0; i < num; i++) \n",
    "\t\t\tif (record[sum] == value[i])\n",
    "\t\t\t\tnum_value[i]++;\n",
    "\t\tsum -= record[sum];\n",
    "\t}\n",
    "\tfor (int i = 0; i < num; i++) {\n",
    "\t\tprintf(\"%d\t%d\\n\", value[i], num_value[i]);\n",
    "\t}\n",
    "}\n",
    " \n",
    " \n",
    "int main() {\n",
    "\tint n; //要找的钱数\n",
    "\tscanf(\"%d\", &n);\n",
    "\tchange(n);\n",
    "\treturn 0;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "money:  [0. 1. 2. 3. 4. 5. 6. 1. 2. 3. 4. 1. 2. 3. 2. 3.]\n",
      "records:  [ 0.  1.  1.  1.  1.  1.  1.  7.  1.  1.  1. 11.  1.  1.  7.  1.]\n",
      "value:  [1, 7, 11]\n",
      "value_num:  [1. 2. 0.]\n"
     ]
    }
   ],
   "source": [
    "n = 15\n",
    "\n",
    "value = [1, 7, 11]\n",
    "\n",
    "money = np.zeros(n+1)\n",
    "records = np.zeros(n+1)\n",
    "\n",
    "value_num = np.zeros(len(value))\n",
    "\n",
    "for i in range(1, n+1):\n",
    "    min_num = n\n",
    "    for j in range(len(value)):\n",
    "        if value[j] <= i and min_num > money[i - value[j]]:\n",
    "            min_num = money[i - value[j]]\n",
    "            ind = j\n",
    "    money[i] = min_num + 1\n",
    "    records[i] = value[ind]\n",
    "\n",
    "while (n > 0):\n",
    "    for j in range(len(value)):\n",
    "        if records[n] == value[j]:\n",
    "            value_num[j] += 1\n",
    "            ind = j\n",
    "    n -= value[ind]\n",
    "\n",
    "print(\"money: \", money)\n",
    "print(\"records: \", records)\n",
    "print(\"value: \", value)\n",
    "print(\"value_num: \", value_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(1, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_values = np.arange(0.2, 8.8, 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2. , 2.2, 2.4, 2.6,\n",
       "       2.8, 3. , 3.2, 3.4, 3.6, 3.8, 4. , 4.2, 4.4, 4.6, 4.8, 5. , 5.2,\n",
       "       5.4, 5.6, 5.8, 6. , 6.2, 6.4, 6.6, 6.8, 7. , 7.2, 7.4, 7.6, 7.8,\n",
       "       8. , 8.2, 8.4, 8.6, 8.8])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "43"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(all_values[:-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_values = all_values[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2. , 2.2, 2.4, 2.6,\n",
       "       2.8, 3. , 3.2, 3.4, 3.6, 3.8, 4. , 4.2, 4.4, 4.6, 4.8, 5. , 5.2,\n",
       "       5.4, 5.6, 5.8, 6. , 6.2, 6.4, 6.6, 6.8, 7. , 7.2, 7.4, 7.6, 7.8,\n",
       "       8. , 8.2, 8.4, 8.6])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "prices = np.arange(0.2, 3.0, 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2. , 2.2, 2.4, 2.6,\n",
       "       2.8])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "43"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(8.6 / 0.2) + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 凑零钱动态规划法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "28.999999999999996"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(8.6 - 2.8) / 0.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4319680.0"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(5399.8 * 2 + 5399.6 * 1 + 5399.2 * 1) * 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "last_price = 5399.8\n",
    "avg_price = 4319680.0 / 200 / 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5399.6"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(avg_price, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "temp_avg_price = 5399.6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "ground = temp_avg_price - 3 * 0.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5399.0"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ground"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "common_price = ground\n",
    "all_prices = np.arange(0.2, last_price-common_price, 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.2, 0.4, 0.6, 0.8])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_prices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "value = round((4319680.0 / 200) - 4 * common_price, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.4"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_values = np.arange(0.2, value, 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_values = list(all_values)\n",
    "all_values.append(2.4)\n",
    "all_values = np.array(all_values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2. , 2.2, 2.4])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(all_values)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "len(money_min) =  13\n",
      "len(records_min) =  13\n",
      "value_num =  [0. 0. 0. 0.]\n",
      "money_min:  [0. 1. 1. 1. 1. 2. 2. 2. 2. 3. 3. 3. 3.]\n",
      "records_min:  [0.  0.2 0.4 0.6 0.8 0.2 0.4 0.6 0.8 0.2 0.4 0.6 0.8]\n",
      "n =  1.8\n",
      "n =  1.6\n",
      "n =  0.8\n",
      "n =  0.0\n",
      "money_min:  [0. 1. 1. 1. 1. 2. 2. 2. 2. 3. 3. 3. 3.]\n",
      "records_min:  [0.  0.2 0.4 0.6 0.8 0.2 0.4 0.6 0.8 0.2 0.4 0.6 0.8]\n",
      "prices:  [0.2 0.4 0.6 0.8]\n",
      "value_num:  [1. 0. 1. 2.]\n"
     ]
    }
   ],
   "source": [
    "n = 2.4\n",
    "\n",
    "money_min = np.zeros(int(round(n / 0.2))+1)\n",
    "\n",
    "print(\"len(money_min) = \", len(money_min))\n",
    "\n",
    "records_min = np.zeros(int(round(n / 0.2))+1)\n",
    "print(\"len(records_min) = \", len(records_min))\n",
    "\n",
    "value_num = np.zeros(len(all_prices))\n",
    "print(\"value_num = \", value_num)\n",
    "\n",
    "for ind, i in enumerate(all_values):\n",
    "    \n",
    "    min_num = n / 0.2\n",
    "    ind += 1\n",
    "    \n",
    "    for j in range(len(all_prices)):\n",
    "        \n",
    "        if prices[j] <= i and min_num > money_min[int(round((i-all_prices[j])/0.2, 1))]:\n",
    "#             print(\"temp\", (i-all_prices[j])/0.2)\n",
    "#             print(\"i = \", i)\n",
    "            min_num = money_min[int(round((i-all_prices[j])/0.2, 1))]\n",
    "            ind_temp_min = j\n",
    "            \n",
    "    money_min[ind] = min_num + 1\n",
    "    records_min[ind] = prices[ind_temp_min]\n",
    "\n",
    "print(\"money_min: \", money_min)\n",
    "# print(\"money_max: \", money_max)\n",
    "print(\"records_min: \", records_min)\n",
    "# print(\"records_max: \", records_max)\n",
    "\n",
    "n = 2.4\n",
    "while (n > 0):\n",
    "    ind = int(n / 0.2)\n",
    "    for j in range(len(all_prices)):\n",
    "        if records_min[ind] == prices[j]:\n",
    "            value_num[j] += 1\n",
    "            ind_temp = j\n",
    "    n -= all_prices[ind_temp]\n",
    "    n = round(n, 1)\n",
    "    print(\"n = \", n)\n",
    "\n",
    "print(\"money_min: \", money_min)\n",
    "print(\"records_min: \", records_min)\n",
    "print(\"prices: \", all_prices)\n",
    "print(\"value_num: \", value_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5399.0"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "common_price "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 背包问题动态规划法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "w = [2, 3, 4, 5]\n",
    "v = [3, 4, 5, 6]\n",
    "s = [1, 1, 1, 3]\n",
    "c = 30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pack3(w, v, s, c):\n",
    "    dp = [0 for _ in range(c+1)]\n",
    "    for i in range(1, len(w)+1):\n",
    "        for j in reversed(range(1, c+1)):\n",
    "            for k in range(s[i-1] + 1):\n",
    "                if k*w[i-1] <= j:\n",
    "                    dp[j] = max(dp[j], dp[j-k*w[i-1]]+k*v[i-1])\n",
    "    return dp[c]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack3(w, v, s, c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "def pack3(w, v, s, c):\n",
    "    for i in range(len(s)):\n",
    "        k = 1\n",
    "        s_value = s[i]\n",
    "        while k<=s_value:\n",
    "            w.append(k*w[i])\n",
    "            v.append(k*v[i])\n",
    "            s_value -= k\n",
    "            k *= 2\n",
    "        if s_value>0:\n",
    "            w.append(s_value*w[i])\n",
    "            v.append(s_value*v[i])\n",
    "    print(\"w = \", w)\n",
    "    print(\"v = \", v)\n",
    "    #前面是划分，后面是0，1背包\n",
    "    dp = [0 for _ in range(c+1)]\n",
    "    for i in range(1, len(w)+1):\n",
    "        for j in reversed(range(1, c+1)):\n",
    "            if w[i-1] <= j:\n",
    "                dp[j] = max(dp[j], dp[j-w[i-1]]+v[i-1])\n",
    "    return dp[c]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w =  [2, 3, 4, 5, 2, 3, 4, 5, 10]\n",
      "v =  [3, 4, 5, 6, 3, 4, 5, 6, 12]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "38"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pack3(w, v, s, c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 问题描述：有 N 个物品，对应 [0.2, 0.4, 0.6, ..., 2.8] 价格序列，也即第一个物品价格为 0.2，最后一个物品价格为 3.0, 物品间的价格间隔为 0.2元。\n",
    "### 现从这 N 个物品中取出 6 个物品(物品可重复)，且其价格之和为 8.6 元，已知有一个物品价格为2.8，求距离该已知价格为 2.8 的物品价格最近的物品价格组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16136.2"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3227240 / 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5374.6"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(5378.6 - 0.2 * 20, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3227000.0"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(5378.2 + 5378.4 * 2) * 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4306600.0"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(5380.6 + 5382.6 + 5384.8 + 5385.0 * 1) * 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = \"\"\"\n",
    "0.  0.2 0.4 0.6 0.8 1.  1.2 1.4 1.6 1.8 2.  2.2 2.4 2.6 2.8 3.  3.2 3.4\n",
    " 3.6 3.8 4.  4.2 4.4 4.6 0.2 0.4 0.6 0.8 1.  1.2 1.4 1.6 1.8 2.  2.2 2.4\n",
    " 2.6 2.8 3.  3.2 3.4 3.6 3.8 4.  4.2 4.4 4.6 0.2 0.4 0.6 0.8 1.  1.2 1.4\n",
    " 1.6 1.8 2.  2.2 2.4 2.6 2.8 3.  3.2 3.4 3.6 3.8 4.  4.2 4.4 4.6 0.2 0.4\n",
    " 0.6 0.8 1.  1.2 1.4 1.6 1.8 2.  2.2\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n0.  0.2 0.4 0.6 0.8 1.  1.2 1.4 1.6 1.8 2.  2.2 2.4 2.6 2.8 3.  3.2 3.4\\n 3.6 3.8 4.  4.2 4.4 4.6 0.2 0.4 0.6 0.8 1.  1.2 1.4 1.6 1.8 2.  2.2 2.4\\n 2.6 2.8 3.  3.2 3.4 3.6 3.8 4.  4.2 4.4 4.6 0.2 0.4 0.6 0.8 1.  1.2 1.4\\n 1.6 1.8 2.  2.2 2.4 2.6 2.8 3.  3.2 3.4 3.6 3.8 4.  4.2 4.4 4.6 0.2 0.4\\n 0.6 0.8 1.  1.2 1.4 1.6 1.8 2.  2.2\\n'"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [],
   "source": [
    "ls = [float(i) for i in a.split()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.4"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ls[68]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.2"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ls[47]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.6"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ls[46]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4.6"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ls[23]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5383600.0"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(5380.6 + 5382.6 + 5384.8 + 5385.0 * 2) * 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_ls = list(np.arange(0.2, 4.8, 0.2)) * 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "metadata": {},
   "outputs": [],
   "source": [
    "temp_value = 16.0\n",
    "\n",
    "ls_ori = list(np.arange(0.2, 4.8, 0.2))\n",
    "ls_ori = [round(i, 1) for i in ls_ori]\n",
    "\n",
    "total_prices = ls_ori\n",
    "\n",
    "records = ls_ori * 3\n",
    "\n",
    "records = records + ls_ori[: 11]\n",
    "\n",
    "records.insert(0, 0)\n",
    "\n",
    "price_num = np.zeros(len(total_prices))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 202,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "80\n",
      "value =  2.2\n",
      "69\n",
      "value =  4.6\n",
      "46\n",
      "value =  4.6\n",
      "23\n",
      "value =  4.6\n"
     ]
    }
   ],
   "source": [
    "while temp_value > 0:\n",
    "    ind = int(round((temp_value /  0.2), 1))\n",
    "    print(ind)\n",
    "    for j in range(len(total_prices)):\n",
    "        if records[ind] == total_prices[j]:\n",
    "            price_num[j] += 1\n",
    "            ind_temp = j\n",
    "    print(\"value = \", total_prices[ind_temp])\n",
    "    temp_value -= total_prices[ind_temp]\n",
    "    temp_value = round(temp_value, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 尝试遍历所有tick价格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26918.0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(5380.6 + 5382.6 + 5384.8 + 5385.0 * 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "prices = np.arange(0.2, 5390, 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26949"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(prices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5389.8"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prices[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "values = np.arange(0.2, 26918.2, 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "values = np.array([round(i, 1) for i in list(values)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26918.0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "values[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for ind, i in enumerate(values):\n",
    "    \n",
    "    min_num = n / 0.2\n",
    "    ind += 1\n",
    "    \n",
    "    for j in range(len(all_prices)):\n",
    "        \n",
    "        if prices[j] <= i and min_num > money_min[int(round((i-all_prices[j])/0.2, 1))]:\n",
    "#             print(\"temp\", (i-all_prices[j])/0.2)\n",
    "#             print(\"i = \", i)\n",
    "            min_num = money_min[int(round((i-all_prices[j])/0.2, 1))]\n",
    "            ind_temp_min = j\n",
    "            \n",
    "    money_min[ind] = min_num + 1\n",
    "    records_min[ind] = prices[ind_temp_min]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
